/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.commons.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import sk.jp.commons.tools.Messages;
import sk.jp.commons.utils.StringUtils;

/**
 * Dialog with scrollable text panel, that presents HTML-formatted text. 
 */
public class TextDialog extends JDialog {

    public enum Msg {
        close("ui.textdialog.btn.close")//Close
        ;
        //----------------------------------------------------------------------
        private String propKey;
        Msg(String propKey) {
            this.propKey = propKey;
        }
        public String getPropKey() {
            return propKey;
        }
        public String value() {
            return Messages.getMessage(propKey);
        }
    }

    //----------------------------------------------------------------------

    private int width = 400;
    private int height = 300;
    private Color bgColor = new Color(240, 240, 240);

    private JScrollPane scrollPane = null;
    private JButton btnClose = null;

    //----------------------------------------------------------------------

    public TextDialog(Frame owner, String title, boolean modal) {
        super(owner, title, modal);
    }

    public TextDialog(Frame owner, String title) {
        super(owner, title);
    }

    public TextDialog(Dialog owner, String title, boolean modal) {
        super(owner, title, modal);
    }

    public TextDialog(Dialog owner, String title) {
        super(owner, title);
    }

    //----------------------------------------------------------------------

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void setBgColor(Color bgColor) {
        this.bgColor = bgColor;
    }

    /**
     * Shows dialog with given text that can contains HTML formatting.
     * @param text text to present. 
     */
    public void open(String text){
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        JEditorPane editorPane = new JEditorPane("text/html", completeText(text));
        editorPane.setEditable(false);
        editorPane.setBackground(bgColor);

        scrollPane = new JScrollPane(editorPane);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
        contentPane.add(scrollPane);

        JPanel btnPane = new JPanel();
        btnPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        btnClose = new JButton(Msg.close.value());
        btnClose.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                dispose();
            }});
        getRootPane().setDefaultButton(btnClose);
        btnPane.add(btnClose);

        Container mainPane = getContentPane();
        mainPane.setLayout(new BorderLayout());
        mainPane.add(contentPane, BorderLayout.CENTER);
        mainPane.add(btnPane, BorderLayout.SOUTH);

        pack();
        setSize(width, height);
        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension sd = t.getScreenSize();
        Dimension dd = getSize();
        setLocation((sd.width - dd.width) / 2, (sd.height - dd.height) / 2);
        setVisible(true);

        scrollPane.getVerticalScrollBar().setValue(0);
        scrollPane.getHorizontalScrollBar().setValue(0);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if(scrollPane != null){
            scrollPane.getVerticalScrollBar().setValue(0);
            scrollPane.getHorizontalScrollBar().setValue(0);
        }
        if(btnClose != null){
            btnClose.requestFocus();
        }
    }

    //----------------------------------------------------------------------

    private String completeText(String text){
        if(text == null) {
            return "";
        }
        text = text.trim();
        if("".equals(text)) {
            return "";
        }
        text = StringUtils.toHtmlString2(text);
        if(!text.substring(0, (text.length() > 6 ? 6 : text.length())).toLowerCase().startsWith("<html>")) {
            return (new StringBuilder()).append("<html>").append(text)
                .append("</html>").toString();
        }
        return text;
    }

}
